// const carList = require("../../../assets/carList.js")
import { carList } from "./carList";
import { getFailNoteList, setFailNoteList,pushFailNoteList } from "../../utils/util" 
let carListCache = carList.splice(0, 10);
console.log("carListCache", carListCache);

function getList(page, pageSize) {
  return carList.splice((page - 1) * pageSize, page * pageSize - 1);
}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    brandName: "",
    checkedTab: "hotSearch",
    tabOptions: [
      {
        label: "热搜",
        value: "hotSearch",
      },
      {
        label: "错题本",
        value: "failNot",
      },
    ],
    page: 1,
    pageSize: 10,
    list: carListCache,
    imgUrl: "http://logo.16888.com/logoimage/2007828224039897.jpg",
  },

  methods: {},
  bindblur: function (e) {
    console.log("bindblur bindblur", e.detail);
  },
  changeTab: function (e) {
    const tab = e.currentTarget.dataset.params;
    this.setData({
      checkedTab: tab,
    });
  },
  onItemBuild(evt) {
    console.log("build", evt.detail.index);
  },

  onItemDispose(evt) {
    console.log("dispose", evt.detail.index);
  },
  addFailNote(e) {
    console.log("e", e);
    const {
      target: {
        dataset: { name },
      },
    } = e;


    console.log("name", name);
    let arr = []
    let brandItem = this.data.list.filter(d => d.field5 === name)?.[0]
    brandItem && arr.push(brandItem) 
    console.log("arr",arr,"brandItem",brandItem);
    pushFailNoteList(brandItem)

  },
  async onChange(e) {
    const tab = e.detail.name;
    this.setData({
      checkedTab: tab,
    });

    let initList = [];
    if (tab === "hotSearch") {
      initList = carListCache;
    } else if (tab === "failNot") {
      initList = await getFailNoteList();
      console.log("initList",initList);
      
    }

    this.setData({
      page: 1,
      list: initList,
    });
  },
  onScrollToLower() {
    wx.showLoading({
      title: "加载中",
    });

    const { page, pageSize } = this.data;

    const list = getList(page, pageSize);
    console.log("this.data.list", this.data.list, "list", list);

    this.setData({
      list: [...this.data.list, ...list],
    });
    setTimeout(function () {
      wx.hideLoading();
    }, 2000);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
